package com.java8.Stream;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class demo03_中间方法 {
    // 中间方法
    //filter 过滤
    @Test
    public void test1(){
        //Stream<T> filter(Predicate predicate)：过滤
//        Predicate接口中的方法	boolean t1(T t)：
//        对给定的参数进行判断，返回一个布尔值
        ArrayList<String> list = new ArrayList<>();
        list.add("张三丰");
        list.add("张无忌");
        list.add("张翠山");
        list.add("王二麻子");
        list.add("张良");
        list.add("谢广坤");
        list.stream().filter(
                new Predicate<String>() {
                    @Override
                    public boolean test(String s) {
                        return s.startsWith("张");
                    }
                }
         ).forEach(s-> System.out.println(s));
        //因为Predicate接口中只有一个抽象方法test,可以使用lambda表达式简化
        list.stream()
                .filter(s -> s.startsWith("张"))
                .forEach(s -> System.out.println(s));
    }
    //limit限制+skip跳过
    @Test
    public void test2(){
        ArrayList<String> list = new ArrayList<>();
        list.add("张三丰");
        list.add("张无忌");
        list.add("张翠山");
        list.add("王二麻子");
        list.add("张良");
        list.add("谢广坤");
        //取前3个
        list.stream().limit(3)
                .forEach(s -> System.out.println(s));
        System.out.println("----------");
        //跳过前3个
        list.stream().skip(3)
                .forEach(s -> System.out.println(s));
        //跳过前3个，再取两个
        System.out.println("----------");
        list.stream().skip(3).limit(2)
                .forEach(s -> System.out.println(s));
    }
    //concat合并两个流和distinct去重
    @Test
    public void test3(){
        ArrayList<String> list1 = new ArrayList<>();
        ArrayList<String> list = new ArrayList<>();
        list.add("张三丰");
        list.add("张无忌");
        list.add("张翠山");
        list.add("王二麻子");
        list.add("张良");
        list.add("谢广坤");
        //先合并再去重
        Stream<String> stream1 = list.stream().limit(3);
        Stream<String> stream2 = list.stream().skip(2);

       Stream.concat(stream1,stream2)
               .distinct()
               .forEach(s -> System.out.println(s));
    }
    //sorted排序和peek(展示)
    @Test
    public void test4(){
        int[] numbers = { 5, 2, 8, 3, 7 };
        int[] sortnumbers = Arrays.stream(numbers)
                .sorted()           //排序
                .peek(num->System.out.print(num+" "))   //展示
                .toArray();

//        System.out.println(Arrays.toString(sortnumbers));
    }
    //map转换
    @Test
    public void test5(){
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> collect = numbers.stream().map(n ->
        {
            n = n * 2;
            return n;
        }).collect(Collectors.toList());
        for (Integer integer : collect) {
            System.out.println("integer = " + integer);
        }
    }
    //转换为map
    @Test
    public void test6(){
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        Map<Byte, Integer> collect = numbers.stream().collect(Collectors.toMap(Integer::byteValue, item -> item*2, (val1, val2) -> val2));
        for (Map.Entry<Byte, Integer> byteIntegerEntry : collect.entrySet()) {
            Byte key = byteIntegerEntry.getKey();
            System.out.println("key = " + key);
            System.out.println("Value = " + byteIntegerEntry.getValue());
        }
    }

}
